package com.yaowk.system.service;

import com.jfinal.aop.Before;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yaowk.authc.Kit.PasswordKit;
import com.yaowk.authc.model.User;
import com.yaowk.system.model.Column;
import com.yaowk.system.model.Module;
import com.yaowk.system.model.Platform;

import java.util.Date;
import java.util.List;

/**
 * @authc yaowk
 * 2017/4/19
 */
public class ModuleService {

    public void initModuleByPlatformId(Integer platformId) {
        Date now = new Date();
        Prop prop = PropKit.use("module.properties");
        String moduleConfig = prop.get("module");
        String separator = prop.get("separator");
        if (StrKit.notBlank(moduleConfig)) {
            String[] modules = moduleConfig.split(separator);
            // 伪删除该平台下的所有模块，逐条根据配置修改或者添加模块
            Module.dao.deleteByPlatformId(platformId);
            for (String m : modules) {
                String moduleName = prop.get(m + ".name");
                String moduleSign = prop.get(m + ".sign");
                String moduleType = prop.get(m + ".type");
                String moduleIcon = prop.get(m + ".icon");
                String moduleColumn = prop.get(m + ".column");
                Module module = Module.dao.findByPlatFormIdAndSign(platformId, moduleSign);
                if (StrKit.notNull(module)) {
                    module.set("type", moduleType)
                            .set("status", 1)
                            .update();
                }
                else {
                    module = new Module();
                    module.set("name", moduleName)
                            .set("sign", moduleSign)
                            .set("type", moduleType)
                            .set("icon", moduleIcon)
                            .set("platform_id", platformId)
                            .set("create_time", now)
                            .save();
                }
                if (StrKit.notBlank(moduleColumn)) {
                    // 伪删除该模块下的所有基于配置的栏目，逐条根据配置修改或者添加栏目
                    Integer moduleId = module.getInt("id");
                    Column.dao.deleteByModuleId(moduleId);
                    String[] columns = moduleColumn.split(separator);
                    for (String c : columns) {
                        String columnName = prop.get(m + "." + c + ".name");
                        String columnSign = prop.get(m + "." + c + ".sign");
                        String columnIcon = prop.get(m + "." + c + ".icon");
                        Column column = Column.dao.findByModuleIdAndSign(moduleId, columnSign);
                        if (StrKit.notNull(column)) {
                            column.set("status", 1)
                                    .update();
                        }
                        else {
                            column = new Column();
                            column.set("name", columnName)
                                    .set("sign", columnSign)
                                    .set("icon", columnIcon)
                                    .set("add_from", "config")
                                    .set("create_time", now)
                                    .set("module_id", moduleId)
                                    .save();
                        }
                    }
                    // 给被删除的栏目添加备注
                    Column.dao.updateRemarkOnInit(moduleId);
                }
            }
            // 给被删除的模块添加备注
            Module.dao.updateRemarkOnInit(platformId);
        }
    }

    @Before(Tx.class)
    public void initModuleByPlatformList(List<Platform> platformList) {
        Platform systemPlatform = Platform.dao.findSystem();
        // 系统平台初始化
        if (systemPlatform == null) {
            systemPlatform = new Platform();
            systemPlatform.setName("系统平台")
                    .setSort(0)
                    .setCreateTime(new Date())
                    .setCreateUser(0)
                    .setSign("system").save();
            systemPlatform.setNo(String.valueOf(systemPlatform.getId())).update();
            // 系统平台管理员初始化
            User user = new User();
            user.setUsername(systemPlatform.getSign() + "admin")
                    .setCreateUser(0)
                    .setCreateTime(new Date())
                    .setType(4)
                    .setName("admin")
                    .setPlatformId(systemPlatform.getId())
                    .setUserCode("admin")
                    .setPassword(PasswordKit.passwordService.encryptPassword("123456"))
                    .save();
            // TODO 系统平台管理员权限初始化
            platformList.add(systemPlatform);
        }
        for (Platform platform : platformList) {
            initModuleByPlatformId(platform.getInt("id"));
        }
    }
}
